from django.db import models

class Node(models.Model):
    label = models.CharField(max_length=200)
    description = models.TextField(blank=True, default='')
    DESC_TYPE_CHOICES = [
        ('plain', '纯文本'),
        ('markdown', 'Markdown'),
        ('html', 'HTML'),
        # 可扩展更多类型
    ]
    desc_type = models.CharField(
        max_length=20,
        choices=DESC_TYPE_CHOICES,
        default='plain',
        help_text='description内容的类型，用于前端渲染方式选择。'
    )
    x_position = models.FloatField(default=0)
    y_position = models.FloatField(default=0)
    size = models.FloatField(default=10)
    color = models.CharField(max_length=20, default='#1f77b4')
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    def __str__(self):
        return f"{self.id}: {self.label}"

class Connection(models.Model):
    source = models.ForeignKey(Node, related_name='outgoing_connections', on_delete=models.CASCADE)
    target = models.ForeignKey(Node, related_name='incoming_connections', on_delete=models.CASCADE)
    weight = models.FloatField(default=1)
    directed = models.BooleanField(default=False)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        unique_together = ('source', 'target')

    def __str__(self):
        return f"{self.source} -> {self.target}"